| species | 2007 | 2008 | 2009 |
|---|---|---|---|
| Adelie | 3750 | NA | NA |
| Adelie | 3800 | NA | NA |
| Adelie | 3250 | NA | NA |
| Adelie | NA | NA | NA |
| Adelie | 3450 | NA | NA |
| Adelie | 3650 | NA | NA |
| Adelie | 3625 | NA | NA |
| Adelie | 4675 | NA | NA |
| Adelie | 3475 | NA | NA |
| Adelie | 4250 | NA | NA |
Gabriela Palomo gabriella.palomo@gmail.com
Dan Herrera
herrerawildlife@gmail.com
After today’s lecture, you’ll be able to:
gglot2 is an R package for creating graphics.
Created by Hadley Wickham and is considered to be part of the tidyverse.
Compose graphs by combining independent components: versatile!
If you learn the grammar then you will end up creating better graphics in less time.
| species | 2007 | 2008 | 2009 |
|---|---|---|---|
| Adelie | 3750 | NA | NA |
| Adelie | 3800 | NA | NA |
| Adelie | 3250 | NA | NA |
| Adelie | NA | NA | NA |
| Adelie | 3450 | NA | NA |
| Adelie | 3650 | NA | NA |
| Adelie | 3625 | NA | NA |
| Adelie | 4675 | NA | NA |
| Adelie | 3475 | NA | NA |
| Adelie | 4250 | NA | NA |
| species | year | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex |
|---|---|---|---|---|---|---|---|
| Adelie | 2007 | Torgersen | 39.1 | 18.7 | 181 | 3750 | male |
| Adelie | 2007 | Torgersen | 39.5 | 17.4 | 186 | 3800 | female |
| Adelie | 2007 | Torgersen | 40.3 | 18.0 | 195 | 3250 | female |
| Adelie | 2007 | Torgersen | NA | NA | NA | NA | NA |
| Adelie | 2007 | Torgersen | 36.7 | 19.3 | 193 | 3450 | female |
| Adelie | 2007 | Torgersen | 39.3 | 20.6 | 190 | 3650 | male |
| Adelie | 2007 | Torgersen | 38.9 | 17.8 | 181 | 3625 | female |
| Adelie | 2007 | Torgersen | 39.2 | 19.6 | 195 | 4675 | male |
| Adelie | 2007 | Torgersen | 34.1 | 18.1 | 193 | 3475 | NA |
| Adelie | 2007 | Torgersen | 42.0 | 20.2 | 190 | 4250 | NA |
Long format data.
Each row is an observation point and each column is a variable.
Data wrangle BEFORE you graph: tidyr::pivot_longer()
ggplot(data = data, mapping = aes(x = x, y = y)) +
geom_*( ) + # geometries: e.g., geom_point(), geom_bar(), ...
coord_*( ) +
facet_*( ) + # dividing your data into facets: facet_grid() and facet_wrap()
scale_*( ) + # controls visual values: colors, fills, shapes. E.g., scale_manual().
theme_*( ) # Controls the overall appearence of the plot: fonts, font size, etc. ggplot()ggplot(): graphing space.
data : data frame or tibble in long format.
aes() : defines the axes and uses column names.
geom_*ggplot(data = data) +
geom_*(aes(x = x,
y = y,
color = z,
fill = f,
shape = w,
linetype = q),
color = color, # points, lines, error bars
shape = shape, # see pch numbers
linetype = linetype, # number, dotted line, dashed line...
fill = fill, # bars, columns, boxplots, violins
alpha=0.3, # transparency
shape = pch, # change the point shape; this is a number or vector of numbers
position = position_dodge() # bar plots are not stacked
) aes() inside ggplot() will be included with all the geometries used in a plot.aes() inside every geom will be included with only that geom. This means that sometimes you might need to specify different or additional aes() when combining different geoms in one plot.aes(): color, fill, shape, alpha (transparency, 0-1), position, size, or linewidth.geom_point(shape = shape) can be specified using any of the pch numbers.geom_line(linetype = linetype) can be specified with either an integer (0-6), a name (0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash.scale_*()xlab('x-axis title') or ylab('y-axis title') or ggtitle('title)labs(title, subtitle, caption, alt)x_lims(c(0,1))scale_*()scale_*() functions can modify:
Position via scale_x_*() or scale_y_*()
Colors via scale_color_*() and scale_fill_*()
Transparency via scale_alpha_*()
Sizes via scale_size_*()
Shapes via scale_shape_*()
* can take the following forms:
Available packages with predefined palettes:
This is a comprehensive list of color palettes in r curated by Emil Hvitfeldt.
Generally, there are 3 types of palettes:
facet_wrap() and facet_grid()facet_*()facet_wrap() and facet_grid().theme_*()Modifies the overall visual defaults of a plot: titles, background color, gridlines, legends,
theme() and theme_*().
theme will help you customize and personalize the overall look of your plot.theme and then customize it with theme_*.theme() will include element_* functions to modify different areas.
theme_classic(), theme_gray(), theme_bw(), theme_linedraw(), theme_light(), theme_dark(), theme_minimal(), theme_void()theme()theme()ggplot(penguins) +
geom_point(aes(x = body_mass_g,
y = flipper_length_mm,
color=island))+
theme(plot.background = element_rect(colour = 'green', fill = 'gray80'),
panel.background = element_rect(colour = 'orange', size = 3, fill = 'pink'),
panel.grid.major = element_line(color = 'blue', size = 2),
legend.position = 'bottom',
axis.title = element_text(size = 20))This is my favorite theme cheatsheet
ggplot(data = data, mapping = aes(x = x, y = y)) +
geom_*( ) + # geometries: e.g., geom_point(), geom_bar(), ...
facet_*( ) + # dividing your data into facets: facet_grid() and facet_wrap()
scale_*( ) + # controls visual values: colors, fills, shapes. E.g., scale_manual().
theme_*( ) # Controls the overall appearence of the plot: fonts, font size, etc. UWIN R Workshops - June 2024